/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.preRegistration.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.preRegistration.domain.PreRegistrationConfig;
import com.fzhucc.modules.preRegistration.domain.dto.PreRegistrationConfigDto;
import com.fzhucc.modules.preRegistration.service.PreRegistrationConfigService;
import com.fzhucc.modules.preRegistration.domain.dto.PreRegistrationConfigQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-01-09
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "预报名:配置管理")
@RequestMapping("/api/preRegistrationConfig")
public class PreRegistrationConfigController {

    private final PreRegistrationConfigService preRegistrationConfigService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('preRegistrationConfig:list')")
    public void exportPreRegistrationConfig(HttpServletResponse response, PreRegistrationConfigQueryCriteria criteria) throws IOException {
        preRegistrationConfigService.download(preRegistrationConfigService.queryAll(criteria), response);
    }

    @AnonymousAccess
    @GetMapping
    @Log("查询预报名配置")
    @ApiOperation("查询预报名配置")
    /*@PreAuthorize("@el.check('preRegistrationConfig:list')")*/
    public ResponseEntity<Object> queryPreRegistrationConfig(PreRegistrationConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(preRegistrationConfigService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增预报名配置")
    @ApiOperation("新增预报名配置")
    @PreAuthorize("@el.check('preRegistrationConfig:add')")
    public ResponseEntity<Object> createPreRegistrationConfig(@Validated @RequestBody PreRegistrationConfig resources){
        return new ResponseEntity<>(preRegistrationConfigService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改预报名配置")
    @ApiOperation("修改预报名配置")
    @PreAuthorize("@el.check('preRegistrationConfig:edit')")
    public ResponseEntity<Object> updatePreRegistrationConfig(@Validated @RequestBody PreRegistrationConfig resources){
        preRegistrationConfigService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除预报名配置")
    @ApiOperation("删除预报名配置")
    @PreAuthorize("@el.check('preRegistrationConfig:del')")
    public ResponseEntity<Object> deletePreRegistrationConfig(@RequestBody Integer[] ids) {
        preRegistrationConfigService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个报名配置")
    @ApiOperation("查询单个报名配置")
    public PreRegistrationConfigDto queryPreRegistrationConfig(@PathVariable Integer id) {
        return preRegistrationConfigService.findById(id);
    }
}